package cn.me.sso.client.service.impl;
import cn.me.sso.client.constants.SsoServerConstants;
import cn.me.sso.client.utils.HttpUtil;
import com.alibaba.fastjson.JSON;
import cn.me.sso.client.service.AuthService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
/**
 * 获取认证授权的service
 * @author zchcpywin10
 */
@Service
public class AuthServiceImpl implements AuthService {
  /**
   * 根据code获取token
   *
   * @param code String
   * @return Map<String, Object>
   */
  @Override
  public Map<String, Object> getAccessToken(String code) {
    String requestUrl = String.format(SsoServerConstants.SSO_TOKEN_URL, code, SsoServerConstants.CLIENT_ID, SsoServerConstants.CLIENT_SECRET, SsoServerConstants.REDIRECT_URL);
    String response = HttpUtil.post(requestUrl, null, null);
    return (Map<String, Object>) JSON.parse(response);
  }
  /**
   * 根据token用户获取信息
   *
   * @param accessToken String
   * @return Map<String, Object>
   */
  @Override
  public Map<String, Object> getUserInfo(String accessToken) {
    Map<String, String> headerMap = new HashMap<>();
    headerMap.put("Authorization", "Bearer " + accessToken);
    String rstr = HttpUtil.get(SsoServerConstants.SSO_USER_URL, headerMap);
    return (Map<String, Object>) JSON.parse(rstr);
  }
}
